/*
	@title   网络请求封装  
	@time    2021-06-02 9:30
*/
export default{
	common:{
		baseUrl:"http://gxyckf.1qit.com/api/",
        // baseUrl:'https://tzdj.1qit.com/',
		header:{
			'Content-Type' : 'application/json;charset=UTF-8',
			"Content-Type": "application/x-www-form-urlencoded",
			// 'Authori-zation':"Bearer " +uni.getStorageSync('token')
			// 'token':'admin2dbdf541-9784-4924-9b73-331a1197746d'
		},
		data:{},
		method:'POST',
		dataType:'json'
	},
	request(options ={}){
		options.url = this.common.baseUrl + options.url
		options.header = {
			'Content-Type' : 'application/json;charset=UTF-8',
			"Content-Type": "application/x-www-form-urlencoded",
			'token':uni.getStorageSync("token")
		}
		// options.header = options.header || this.common.header
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		return new Promise((res,rej)=>{
			uni.request({
				...options,
				success: (result) => {
					res(result)
					// result.data.data.code==-1
					if(result.data.data.code==-1){
						uni.showToast({
						    title: 'token过期，需要重新登录',
						    duration: 2000
						});
						uni.navigateTo({
							url:'/pages/login/index'
						})
					}
				},
				fail: (error) => {
					console.log(error)
					if(error.data.data.code==-1){
						uni.showToast({
						    title: 'token过期，需要重新登录',
						    duration: 2000
						});
						uni.reLaunch({
							url:'/pages/login/index'
						})
					}
				}
			});
		})


		// return new Promise((res,rej)=>{
		// 	uni.request({
		// 		...options,
		// 		success: (result) => {
		// 			if(result.data.code === 401){
		// 				uni.showToast({
		// 					title:"登陆过期，请重新登录",
		// 					icon:"none",
		// 					duration:1500
		// 				})
		// 				setTimeout(()=>{
		// 					uni.navigateTo({
		// 						url:"/pages/login/index"
		// 					})
		// 				},1500)
		// 			}
        //             res(result)
		// 		},
		// 		fail: (error) => {
		// 			console.log(error)
		// 		}
		// 	});
		// })

	},
	post(url,data={},options={}){
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	get(url,data={},options={}){
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	}
}